home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_high.lha / LEDA-3.1c-high / man / GRAPH.tex < prev    next >
Encoding:
Text File  |  1994-08-05  |  12.1 KB  |  291 lines

  1. \bigskip
  2. {\magonebf 5.1 Directed graphs (graph)}
  3.  
  4. {\bf 1. Definition}
  5.  
  6. An instance $G$ of the data type $graph$ consists of a list of nodes $V$ and a 
  7. list of edges E ($node$ and $edge$ are predefined data types). Every edge $e\in 
  8. E$ is a pair of nodes $(v,w) \in V\times V$, $v$ is called the source of $e$ and
  9. $w$ is called the target of $e$. With every node $v$ the list of its adjacent 
  10. edges $adj\_list(v) = \{\ e \in E\ | source(e) = v\ \}$, called the adjacency 
  11. list of $v$, is associated.
  12.  
  13. \def\name{$graph$}
  14. \def\type{graph}
  15.  
  16. \bigskip
  17. {\bf 2. Creation}
  18.  
  19. \create G {}
  20.  
  21. creates an instance $G$ of type $graph$ and initializes it to the
  22. empty graph.
  23.  
  24.  
  25. \bigskip
  26. {\bf 3. Operations}
  27.  
  28. {\bf a) Access operations}
  29. \+\cleartabs & \hskip 2truecm & \hskip 5.5truecm &\cr
  30. \+\op int        indeg {node\ v}                     
  31.                                       {returns the indegree of node $v$}
  32. \smallskip
  33. \+\op int        outdeg {node\ v}                     
  34.                                       {returns the outdegree of node $v$}
  35. \smallskip
  36. \+\op node       source {edge\ e}               
  37.                                       {returns the source node of edge $e$}
  38. \smallskip
  39. \+\op node       target {edge\ e}               
  40.                                       {returns the target node of edge $e$}
  41. \smallskip
  42. \+\op int        number\_of\_nodes { }
  43.                              {returns the number of nodes in $G$ }
  44. \smallskip
  45. \+\op int        number\_of\_edges { }
  46.                              {returns the number of edges in $G$ }
  47. \smallskip
  48. \+\op list\<node\> all\_nodes { }
  49.                              {returns the list $V$ of all nodes of $G$}
  50. \smallskip
  51. \+\op node         first\_node { }
  52.                              {returns the first node in $V$}
  53. \smallskip
  54. \+\op node         last\_node { }
  55.                              {returns the last node in $V$}
  56. \smallskip
  57. \+\op node         succ\_node {node\ v}
  58.                             {returns the successor of node $v$ in $V$ }
  59. \+\nop                      {(nil if it does not exist)}
  60. \smallskip
  61. \+\op node         pred\_node {node\ v}
  62.                             {returns the predecessor of node $v$ in $V$ }
  63. \+\nop                      {(nil if it does not exist)}
  64. \smallskip
  65. \+\op list\<edge\> all\_edges { }
  66.                              {returns the list $E$ of all edges of $G$}
  67. \smallskip
  68. \+\op edge         first\_edge { }
  69.                              {returns the first edge in $E$}
  70. \smallskip
  71. \+\op edge         last\_edge { }
  72.                              {returns the last edge in $E$}
  73. \smallskip
  74. \+\op edge         succ\_edge {edge\ e}
  75.                             {returns the successor of edge $e$ in $E$ }
  76. \+\nop                      {(nil if it does not exist)}
  77. \smallskip
  78. \+\op edge         pred\_edge {edge\ e}
  79.                             {returns the predecessor of edge $e$ in $E$ }
  80. \+\nop                      {(nil if it does not exist)}
  81. \smallskip
  82. \+\op list\<edge\> adj\_edges {node\ v}  
  83.                              {returns the list of all edges adjacent to $v$}
  84. \smallskip
  85. \+\op list\<node\> adj\_nodes {node\ v}  
  86.                              {returns the list of all nodes adjacent to $v$}
  87. \smallskip
  88. \+\op edge   first\_adj\_edge {node\ v}
  89.                            {returns the first edge in the adjacency list of $v$}
  90. \smallskip 
  91. \+\op edge   last\_adj\_edge {node\ v} 
  92.                            {returns the last edge in the adjacency list of $v$}
  93. \smallskip
  94. \+\op edge   adj\_succ {edge\ e}           
  95.                             {returns the successor of edge $e$ in the }
  96. \+\nop                      {adjacency list of $source(e)$ }
  97. \+\nop                      {(nil if it does not exist)}
  98. \smallskip
  99. \+\op edge   adj\_pred {edge\ e}          
  100.                              {returns the predecessor of edge $e$ in the }
  101. \+\nop                       {adjacency list of $source(e)$}
  102. \+\nop                       {(nil if it does not exist)}
  103. \smallskip
  104. \+\op edge   cyclic\_adj\_succ {edge\ e}    
  105.                              {returns the cyclic successor of edge $e$ in the}
  106. \+\nop                       {adjacency list of $source(e)$}
  107. \smallskip
  108. \+\op edge   cyclic\_adj\_pred {edge\ e}    
  109.                              {returns the cyclic predecessor of edge $e$ in the}
  110. \+\nop                       {adjacency list of $source(e)$}
  111. \smallskip
  112. \+\op node   choose\_node   {}
  113.                               {returns a node of $G$ (nil if $G$ is empty)}
  114. \smallskip
  115. \+\op edge   choose\_edge   {}
  116.                               {returns an edge of $G$ (nil if $G$ is empty)}
  117.  
  118. \bigskip
  119. {\bf b) Update operations}
  120. \medskip
  121. \+\op node   new\_node {}   
  122.                              {adds a new node to $G$ and returns it}
  123. \smallskip
  124. \+\op void   del\_node {node\ v}      
  125.                              {deletes $v$ and all edges adjacent to $v$}
  126. \+\nop                       {from $G$. \precond{$indeg(v) = 0$}.}
  127. \smallskip
  128. \+\op edge   new\_edge {node\ v,\ w}
  129.                              {adds a new edge $(v,w)$ to $G$ by appending }
  130. \+\nop                       {it to the adjacency list of $v$ and returns it.}
  131. \smallskip
  132. \+\op edge   new\_edge {edge\ e,\ node\  w,\ rel\_pos\ dir=after} {}
  133. \+\nop                      {adds a new edge $e\' = (source(e),w)$ to $G$ by }
  134. \+\nop                      {inserting it after ($dir$=after) or before ($dir$}
  135. \+\nop                      {=before) edge $e$ into the adjacency list of }
  136. \+\nop                      {$source(e)$, returns $e\'$.}
  137. \smallskip
  138. \+\op void   del\_edge {edge\ e}      
  139.                              {deletes the edge $e$ from $G$}
  140. \smallskip
  141. \+\op void       del\_all\_nodes {}
  142.                              {deletes all nodes from $G$ }
  143. \smallskip
  144. \+\op void       del\_all\_edges {}
  145.                              {deletes all edges from $G$ }
  146. \smallskip
  147. \+\op edge   rev\_edge {edge\ e}      
  148.                              {reverses the edge $e = (v,w)$ by removing it }
  149. \+\nop                       {from $G$ and inserting the edge $e\' = (w,v)$ }
  150. \+\nop                       {into $G$ by appending it to the adjacency list}
  151. \+\nop                       {of $w$, returns $e\'$ }
  152. \smallskip
  153. \+\op void   rev {}                 
  154.                              {all edges in $G$ are reversed}
  155. \smallskip
  156. \+\op void   sort\_nodes {int (*cmp)(node\&,\ node\&)}  {}
  157. \+\nop                       {the nodes of $G$ are sorted according to the}
  158. \+\nop                       {ordering defined by the comparing function}
  159. \+\nop                       {$cmp$. Subsequent executions of forall\_nodes}
  160. \+\nop                       {step through the nodes in this order.}
  161. \+\nop                       { (cf.~TOPSORT1 in section 8.1)}
  162. \smallskip
  163. \+\op void   sort\_nodes {node\_array\<T\>\ A}  {}
  164. \+\nop                       {the nodes of $G$ are sorted according to the}
  165. \+\nop                       {entries of node\_array $A$ (cf.~section 5.7)}
  166. \+\nop                       {\precond $T$ must be linearly ordered}
  167. \smallskip
  168. \+\op void   sort\_edges {int (*cmp)(edge\&,\ edge\&)}  {}
  169. \+\nop                       {the edges of $G$ are sorted according to the}
  170. \+\nop                       {ordering defined by the comparing function}
  171. \+\nop                       {$cmp$. Subsequent executions of forall\_edges}
  172. \+\nop                       {step through the edges in this order.}
  173. \+\nop                       { (cf.~TOPSORT1 in section 8.1)}
  174. \smallskip
  175. \+\op void   sort\_edges {edge\_array\<T\>\ A}  {}
  176. \+\nop                       {the edges of $G$ are sorted according to the}
  177. \+\nop                       {entries of edge\_array $A$ (cf.~section 5.7)}
  178. \+\nop                       {\precond $T$ must be linearly ordered}
  179. \smallskip
  180. \+\op list\<edge\> insert\_reverse\_edges {} {}
  181. \+\nop                       {for every edge $(v,w)$ in $G$ the reverse edge}
  182. \+\nop                       {$(w,v)$ is inserted into $G$. The list of all}
  183. \+\nop                       {inserted edges is returned.}
  184. \smallskip
  185. \+\op void   make\_undirected {}
  186.                              {every edge $(v,w)$ in $G$ is inserted into the}
  187. \+\nop                       {adjacency list of $w$.}
  188. \smallskip
  189. \+\op void   make\_directed {}
  190.                              {every edge $(v,w)$ in $G$ is removed from the}
  191. \+\nop                       {adjacency list of $w$.}
  192. \smallskip
  193. \+\op void   clear {}              
  194.                              {makes $G$ the empty graph}
  195.  
  196. \vfill\eject
  197.  
  198. \bigskip
  199. {\bf c) Iterators}
  200. \medskip
  201. With the adjacency list of every node $v$ is associated a list iterator 
  202. called the adjacency iterator of $v$ (cf.~list). There are operations to 
  203. initialize, move, and read these iterators. They are used to implement 
  204. iteration statements (forall\_adj\_edges, forall\_adj\_nodes). 
  205. \bigskip
  206. \+\op void   init\_adj\_iterator {node\ v}         
  207.                       {assigns nil to the adjacency iterator of node $v$}
  208. \smallskip
  209. \+\op bool   current\_adj\_edge {edge\&\ e,\ node\ v}  {}
  210. \+\nop                {if the adjacency iterator of $v$ is defined ($\ne$ nil)} 
  211. \+\nop                {its contents is assigned to $e$ and true is returned}
  212. \+\nop                {else false is returned.}
  213. \smallskip
  214. \+\op bool   next\_adj\_edge {edge\&\ e,\ node\ v}     {}
  215. \+\nop                {moves the adjacency iterator of $v$ forward (to the}
  216. \+\nop                {first item of $adj\_list(v)$ if it is nil) and returns}
  217. \+\nop                {$G$.current\_adj\_edge($e,v$)}
  218. \smallskip
  219. \+\op bool   current\_adj\_node {node\&\ w,\ node\ v}  {}
  220. \+\nop                {if $G$.current\_adj\_edge($e$, $v$) = true then assign}
  221. \+\nop                {$target(e)$ to w and return true, else return}
  222. \+\nop                {false}
  223. \smallskip
  224. \+\op bool   next\_adj\_node {node\&\ w,\ node\ v}     {}
  225. \+\nop                {if $G$.next\_adj\_edge($e$, $v$) = true then assign}
  226. \+\nop                {$target(e)$ to w and return true, else return}
  227. \+\nop                {false}
  228. \smallskip
  229. \+\op void   reset {}                          
  230.                       {assign nil to all adjacency iterators in $G$}
  231.  
  232. \bigskip
  233. {\bf d) Miscellaneous operations}
  234. \medskip
  235. \+\op void   write {ostream\ O\ =\ cout}        
  236.                              {writes a compressed representation of $G$ to }
  237. \+\nop                       {the output stream $O$.}
  238. \smallskip
  239. \smallskip
  240. \+\op void   write {string\ s}        
  241.                              {writes a compressed representation of $G$ to }
  242. \+\nop                       {the file with name $s$.}
  243. \smallskip
  244. \+\op void   read {istream\ I\ =\ cin}         
  245.                              {reads a compressed representation of $G$ from }
  246. \+\nop                       {the input stream $I$.}
  247. \smallskip
  248. \+\op void   read {string\ s}         
  249.                              {reads a compressed representation of $G$ from }
  250. \+\nop                       {the file with name $s$.}
  251. \smallskip
  252. \+\op void   print\_node {node\ v,\ ostream\ O = cout} {}              
  253. \+\nop                       {writes a readable representation of node $v$ to }
  254. \+\nop                       {the output stream $O$}
  255. \smallskip
  256. \+\op void   print\_edge {edge\ e,\ ostream\ O = cout} {}              
  257. \+\nop                       {writes a readable representation of edge $e$ to}
  258. \+\nop                       {the output stream $O$}
  259. \smallskip
  260. \+\op void   print {ostream\ O = cout}              
  261.                              {writes a readable representation of $G$ to the}
  262. \+\nop                       {output stream $O$}
  263.  
  264.  
  265. \bigskip
  266. {\bf 4. Iteration}
  267.  
  268. {\bf forall\_nodes}($v,G$) 
  269. $\{$ ``the nodes of $G$ are successively assigned to $v$" $\}$
  270.  
  271. {\bf forall\_edges}($e,G$)  
  272. $\{$ ``the edges of $G$ are successively assigned to $e$" $\}$
  273.  
  274. {\bf forall\_adj\_edges}($e,w$) \nl
  275. \phantom{{\bf forall\_edges}($v,G$)}
  276. $\{$ ``the edges adjacent to node w are successively assigned to e" $\}$
  277.  
  278. {\bf forall\_adj\_nodes}($v,w$) \nl  
  279. \phantom{{\bf forall\_edges}($v,G$)}
  280. $\{$ ``the nodes adjacent to node w are successively assigned to v" $\}$
  281.  
  282. \bigskip
  283. {\bf 5. Implementation}
  284.  
  285. Graphs are implemented by doubly linked adjacency lists. Most operations 
  286. take constant time, except of all\_nodes, all\_edges, del\_all\_nodes,
  287. del\_all\_edges, clear, write, and read which take time $O(n+m)$, where 
  288. $n$ is the current number of nodes and $m$ is the current number of edges. 
  289. The space requirement is $O(n+m)$.
  290.  
  291.